package com.chartdemo.view;

import org.achartengine.GraphicalView;
import org.achartengine.chart.AbstractChart;
import org.achartengine.chart.PieChart;
import org.achartengine.renderer.DefaultRenderer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * 饼状图
 * 
 * @author huangtg
 * 
 */
public class PieChartView extends GraphicalView
{
	/** The zoom buttons background color. */
	private static final int ZOOM_BUTTONS_COLOR = Color.argb(175, 150, 150, 150);
	
	/**
	 * 矩阵
	 */
	private Rect mRect = new Rect();
	
	/**
	 * scan smaller
	 */
	private Bitmap zoomInImage;
	
	/**
	 * scan bigger
	 */
	private Bitmap zoomOutImage;
	
	/**
	 * scan fit
	 */
	private Bitmap fitZoomImage;
	/** The zoom area size. */
	private int zoomSize = 50;
	/** The zoom buttons rectangle. */
	private RectF mZoomR = new RectF();
	
	/**
	 * 画笔
	 */
	private Paint mPaint = new Paint();
	
	/**
	 * PieChart
	 */
	private PieChart mChart;
	
	/**
	 * mRenderer
	 */
	private DefaultRenderer renderer;
	
	public PieChartView(Context context, AbstractChart chart)
	{
		super(context, chart);
		mChart = (PieChart) chart;
		init();
	}
	
	public void init()
	{
		zoomInImage = BitmapFactory
				.decodeStream(GraphicalView.class.getResourceAsStream("image/zoom_in.png"));
		zoomOutImage = BitmapFactory.decodeStream(GraphicalView.class
				.getResourceAsStream("image/zoom_out.png"));
		fitZoomImage = BitmapFactory
				.decodeStream(GraphicalView.class.getResourceAsStream("image/zoom-1.png"));
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		canvas.getClipBounds(mRect);
		int top = mRect.top;
		int left = mRect.left;
		int width = mRect.width();
		int height = mRect.height();
		if (true)
		{
			top = 0;
			left = 0;
			width = getMeasuredWidth();
			height = getMeasuredHeight();
		}
		mChart.draw(canvas, left, top, width, height, mPaint);
		if (renderer != null && renderer.isZoomEnabled() && renderer.isZoomButtonsVisible())
		{
			mPaint.setColor(ZOOM_BUTTONS_COLOR);
			zoomSize = Math.max(zoomSize, Math.min(width, height) / 7);
			mZoomR.set(left + width - zoomSize * 3, top + height - zoomSize * 0.775f, left + width, top
					+ height);
			canvas.drawRoundRect(mZoomR, zoomSize / 3, zoomSize / 3, mPaint);
			float buttonY = top + height - zoomSize * 0.625f;
			canvas.drawBitmap(zoomInImage, left + width - zoomSize * 2.75f, buttonY, null);
			canvas.drawBitmap(zoomOutImage, left + width - zoomSize * 1.75f, buttonY, null);
			canvas.drawBitmap(fitZoomImage, left + width - zoomSize * 0.75f, buttonY, null);
		}
	}
}
